﻿Imports DAL
Public Class FrmEmployePicker
    Private listeEmploye As New List(Of Employe)

    Private _SelectedEmployes As New List(Of Employe)
    Public Property SelectedEmployes() As List(Of Employe)
        Get
            _SelectedEmployes.Clear()
            Dim selectedID As New List(Of Integer)
            For Each itm As ListViewItem In Lv_SelectEmp.Items
                selectedID.Add(Convert.ToInt32(itm.Tag))
            Next
            Using dbc As New BDCarpeDiemDataContext
                _SelectedEmployes = (From x In dbc.Employes Where (x.Supprime = False) _
                                AndAlso selectedID.Contains(x.ID) Select x).ToList()
            End Using
            Return _SelectedEmployes
        End Get
        Set(ByVal value As List(Of Employe))
            _SelectedEmployes = value
            For Each emp In _SelectedEmployes
                Dim itm As New ListViewItem(emp.Nom.TrimStart() + ", " + emp.Prenom)
                itm.Tag = emp.ID
                itm.Name = "lv_item_" + itm.Tag.ToString()
                Lv_SelectEmp.Items.Add(itm)
            Next
        End Set
    End Property
    Private _InclureSupprimer As Boolean = False
    Public Property InclureSupprimer() As Boolean
        Get
            Return _InclureSupprimer
        End Get
        Set(ByVal value As Boolean)
            _InclureSupprimer = value
        End Set
    End Property

    Private _SingleSelection As Boolean
    Public Property SingleSelection() As String
        Get
            Return _SingleSelection
        End Get
        Set(ByVal value As String)
            _SingleSelection = value
        End Set
    End Property


    Private Sub FrmEmployePicker_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Using dbc As New BDCarpeDiemDataContext
            listeEmploye = (From x In dbc.Employes Where (x.Supprime = False Or x.Supprime = InclureSupprimer) Select x).ToList()

        End Using
        For Each emp In listeEmploye
            Dim itm As New ListViewItem(emp.Nom.TrimStart() + ", " + emp.Prenom)
            itm.Tag = emp.ID
            Lv_AllEmp.Items.Add(itm)
        Next
    End Sub

    Private Sub Btn_Operation_Cick(sender As System.Object, e As System.EventArgs) Handles Btn_RemoveAll.Click, Btn_Remove.Click, Btn_AddAll.Click, Btn_Add.Click
        Select Case CType(sender, Button).Text
            Case ">"
                For Each itm As ListViewItem In Lv_AllEmp.SelectedItems
                    Dim nitm As New ListViewItem(itm.Text)
                    nitm.Tag = itm.Tag
                    nitm.Name = "lv_item_" + itm.Tag.ToString()
                    If Not Lv_SelectEmp.Items.ContainsKey(nitm.Name) Then
                        Lv_SelectEmp.Items.Add(nitm)
                    End If
                Next
            Case ">>"
                For Each itm As ListViewItem In Lv_AllEmp.Items
                    Dim nitm As New ListViewItem(itm.Text)
                    nitm.Tag = itm.Tag
                    nitm.Name = "lv_item_" + itm.Tag.ToString()
                    If Not Lv_SelectEmp.Items.ContainsKey(nitm.Name) Then
                        Lv_SelectEmp.Items.Add(nitm)
                    End If
                Next
            Case "<"
                While Lv_SelectEmp.SelectedItems.Count > 0
                    Lv_SelectEmp.Items.Remove(Lv_SelectEmp.SelectedItems(0))
                End While
            Case "<<"
                Lv_SelectEmp.Items.Clear()
        End Select

    End Sub

    Private Sub Btn_Accepter_Click(sender As System.Object, e As System.EventArgs) Handles Btn_Accepter.Click
        If SingleSelection AndAlso Lv_SelectEmp.Items.Count <> 1 Then
            MessageBox.Show("Veuillez sélectionner un employé.")
            Return
        End If
        Me.DialogResult = Windows.Forms.DialogResult.OK
    End Sub
End Class